<template>
  <view class="application-container">
    <view class="filter-bar">
      <picker @change="statusChange" :value="statusIndex" :range="statusOptions">
        <view class="filter-item">
          {{ statusOptions[statusIndex] }}
          <uni-icons type="arrowdown" size="14" color="#666"></uni-icons>
        </view>
      </picker>
    </view>

    <view class="application-list">
      <view 
        class="application-item"
        v-for="item in filteredApplications"
        :key="item.id"
        :class="item.status === '待审核' ? 'pending' : item.status === '已通过' ? 'approved' : 'rejected'"
      >
        <view class="header">
          <text class="school">{{ item.schoolName }}</text>
          <text class="status">{{ statusMap[item.status] }}</text>
        </view>
        <view class="content">
          <view class="info-item">
            <uni-icons type="location" size="16" color="#666"></uni-icons>
            <text>{{ item.location }}</text>
          </view>
          <view class="info-item">
            <uni-icons type="calendar" size="16" color="#666"></uni-icons>
            <text>{{ item.applyDate }}</text>
          </view>
        </view>
        <view class="footer">
          <button 
            class="action-btn" 
            @click="cancelApplication(item.id)"
            v-if="item.status === 'pending'"
          >
            取消申请
          </button>
          <button 
            class="action-btn" 
            @click="viewDetails(item.id)"
          >
            查看详情
          </button>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      statusIndex: 0,
      statusOptions: ['全部状态', '待审核', '已通过', '已拒绝'],
      statusMap: {
        '待审核': '审核中',
        '已通过': '已通过',
        '已拒绝': '已拒绝'
      },
      applications: [],
      filteredApplications: []
    }
  },
  onLoad() {
    this.loadApplications()
    // 监听申请更新事件
    uni.$on('application-updated', this.loadApplications)
  },
  onUnload() {
    // 移除事件监听
    uni.$off('application-updated', this.loadApplications)
  },
  methods: {
    // 加载申请记录
    loadApplications() {
      const applications = uni.getStorageSync('applications') || []
      
      // 格式化日期
      this.applications = applications.map(item => {
        return {
          ...item,
          applyDate: this.formatDate(item.createTime),
          schoolName: item.postTitle || '未知项目'
        }
      })
      
      // 应用筛选
      this.filterApplications()
    },
    
    // 格式化日期
    formatDate(dateString) {
      if (!dateString) return '未知时间'
      const date = new Date(dateString)
      return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
    },
    
    // 筛选申请记录
    filterApplications() {
      if (this.statusIndex === 0) {
        // 全部状态
        this.filteredApplications = [...this.applications]
      } else {
        const status = this.statusOptions[this.statusIndex]
        this.filteredApplications = this.applications.filter(item => {
          if (status === '待审核' && item.status === '待审核') return true
          if (status === '已通过' && item.status === '已通过') return true
          if (status === '已拒绝' && item.status === '已拒绝') return true
          return false
        })
      }
    },
    
    // 状态变更
    statusChange(e) {
      this.statusIndex = e.detail.value
      this.filterApplications()
    },
    
    // 取消申请
    cancelApplication(id) {
      uni.showModal({
        title: '确认取消',
        content: '确定要取消该申请吗？',
        success: (res) => {
          if (res.confirm) {
            const applications = uni.getStorageSync('applications') || []
            const index = applications.findIndex(item => item.id === id)
            
            if (index > -1) {
              // 删除申请
              applications.splice(index, 1)
              uni.setStorageSync('applications', applications)
              
              // 刷新列表
              this.loadApplications()
              
              uni.showToast({
                title: '已取消申请',
                icon: 'success'
              })
            }
          }
        }
      })
    },
    
    // 查看详情
    viewDetails(id) {
      uni.navigateTo({
        url: `/pages/profile/application-detail?id=${id}`
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.application-container {
  padding: 30rpx;
  
  .filter-bar {
    margin-bottom: 30rpx;
    
    .filter-item {
      background: #fff;
      border-radius: 40rpx;
      padding: 15rpx 30rpx;
      display: inline-flex;
      align-items: center;
    }
  }
  
  .application-item {
    background: #fff;
    border-radius: 16rpx;
    padding: 30rpx;
    margin-bottom: 20rpx;
    
    &.pending .status {
      color: #5bc0eb;
    }
    &.approved .status {
      color: #4cd964;
    }
    &.rejected .status {
      color: #ff3b30;
    }
    
    .header {
      display: flex;
      justify-content: space-between;
      margin-bottom: 20rpx;
      
      .school {
        font-size: 32rpx;
        color: #2c3e50;
      }
      .status {
        font-size: 26rpx;
      }
    }
    
    .content {
      .info-item {
        display: flex;
        align-items: center;
        margin-bottom: 15rpx;
        color: #666;
        font-size: 26rpx;
        
        uni-icons {
          margin-right: 10rpx;
        }
      }
    }
    
    .footer {
      margin-top: 30rpx;
      display: flex;
      gap: 20rpx;
      
      .action-btn {
        flex: 1;
        background: rgba(91, 192, 235, 0.1);
        color: #5bc0eb;
        border-radius: 40rpx;
        font-size: 26rpx;
        
        &:last-child {
          background: #f5f5f5;
          color: #666;
        }
      }
    }
  }
}
</style>